package com.syezon.wifikey.config;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;

import com.blankj.utilcode.util.SPUtils;
import com.syezon.wifikey.utils.ClientInfoUtils;
import com.syezon.wifikey.utils.SpUtil;
import com.syezon.wifikey.utils.Tools;

/**
 * app 配置信息
 */

public class AppConfig {

    public static final int APP_ID = 28;

    public static final String FILE_NAME = "config";

    public static boolean IS_LINE_UP;//判断是否首发
    public static String CHANNEL;//渠道
    public static String IMEI;
    public static String PACKAGE_NAME;
    public static int VERSION_CODE;
    public static String VERSION_NAME = "";
    public static int USER_TYPE;

    public static final String NOTIFY_STATE = "notify_state";
    public static final String PERCENT_GH = "percent_gonghao";

    public static void getAppConfig(Context context) {
        if (context != null) {
            CHANNEL = getMetaDate(context, "UMENG_CHANNEL");
            IS_LINE_UP = getMetaDate(context, "FIRST").equals("yes");
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            IMEI = ClientInfoUtils.getImei();
            if (IMEI == null) {
                if (SpUtil.getImeiDefault(context) != "") {
                    IMEI = SpUtil.getImeiDefault(context);
                } else {
                    IMEI = "wifi" + Tools.getRandom(1, 9998) + Tools.getRandom(1, 9998) + Tools.getRandom(1, 9998);
                    SpUtil.saveImeiDefault(context, IMEI);
                }
            }

            // 获取应用信息
            PackageManager pm = context.getPackageManager();
            PackageInfo pi;
            PACKAGE_NAME = context.getPackageName();
            try {
                pi = pm.getPackageInfo(PACKAGE_NAME, 0);
                VERSION_CODE = pi.versionCode;
                VERSION_NAME = pi.versionName;
            } catch (Exception e) {

            }
        }
    }


    /**
     * 读取AndroidManifest.xml中<meta-data>元素数据
     */
    public static String getMetaDate(Context context, String key) {
        try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            String value = info.metaData.getString(key);
            return value == null ? "" : value;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static boolean getNotifyState() {
        return (boolean) SpConfigManager.get(FILE_NAME, NOTIFY_STATE, false);
    }

    public static void saveNotifyState(boolean isNotify) {
        SpConfigManager.put(FILE_NAME, NOTIFY_STATE, isNotify);
    }

    public static int getGongHao() {
        return (int) SpConfigManager.get(FILE_NAME, PERCENT_GH, 70);
    }

    public static void saveGongHao(int googhao) {
        SpConfigManager.put(FILE_NAME, PERCENT_GH, googhao);
    }

    private static final String CONFIG_NAME = "torch_config";
    private static final String NEWS_TAB_CLICK_DAY = "news_tab_click_day";
    private static final String IS_FIRST = "is_first";
    private final static String AGREEMENT_STATUS = "agreement_status";
    private final static String SHOW_NEWS_TAB = "show_news_tab";

    /**
     * @return 获取新闻tab点击的日期
     */
    public static int getNewsClickDay() {
        return SPUtils.getInstance(CONFIG_NAME).getInt(NEWS_TAB_CLICK_DAY, 0);
    }

    /**
     * @param day 保存新闻tab点击的日期
     */
    public static void saveNewsClickDay(int day) {
        SPUtils.getInstance(CONFIG_NAME).put(NEWS_TAB_CLICK_DAY, day);
    }

    /**
     * @return 获取是否时第一次使用app
     */
    public static boolean isFirst() {
        return SPUtils.getInstance(CONFIG_NAME).getBoolean(IS_FIRST, true);
    }

    /**
     * @param isFirst 保存是否时第一次使用app
     */
    public static void setFirst(boolean isFirst) {
        SPUtils.getInstance(CONFIG_NAME).put(IS_FIRST, isFirst);
    }

    /**
     * @param agree 用户是否同意用户协议
     */
    public static void saveAgreement(boolean agree) {
        SPUtils.getInstance(CONFIG_NAME).put(AGREEMENT_STATUS, agree);
    }

    /**
     * @return 是否同意用户协议
     */
    public static boolean isAgree() {
        return SPUtils.getInstance(CONFIG_NAME).getBoolean(AGREEMENT_STATUS, false);
    }
    /**
     * @param showNewsTab 用户是否展示新闻tab
     */
    public static void saveShowNewsTab(boolean showNewsTab) {
        SPUtils.getInstance(CONFIG_NAME).put(SHOW_NEWS_TAB, showNewsTab);
    }

    /**
     * @return 是否展示新闻tab
     */
    public static boolean isShowNewsTab() {
        return SPUtils.getInstance(CONFIG_NAME).getBoolean(SHOW_NEWS_TAB, true);
    }
}
